package com.hzjh.base.style.view.round

import android.content.Context
import android.graphics.Canvas
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.widget.LinearLayout
import com.hzjh.base.style.R

class RoundKornerLinearLayout
@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr) {
    private val canvasRounder: CanvasRounder

    /**
     * 设置ImageView的宽高比例，需要将width或height中的一个设置为0
     * 默认值为0，表示不设置
     * 例如：
     * width = match_parent
     * height = 0
     * ratio = 0.5
     * height高度将为width高度的1/2
     */
    private var dimensionRatio: Float = 0f

    init {
        val array = context.obtainStyledAttributes(attrs, R.styleable.RoundKornerLinearLayout, 0, 0)
        val cornersHolder = array.getCornerRadius(
            R.styleable.RoundKornerLinearLayout_corner_radius,
            R.styleable.RoundKornerLinearLayout_top_left_corner_radius,
            R.styleable.RoundKornerLinearLayout_top_right_corner_radius,
            R.styleable.RoundKornerLinearLayout_bottom_right_corner_radius,
            R.styleable.RoundKornerLinearLayout_bottom_left_corner_radius,
            R.styleable.RoundKornerLinearLayout_rd_border_width,
            R.styleable.RoundKornerLinearLayout_rd_border_color
        )
        dimensionRatio = array.getFloat(R.styleable.RoundKornerLinearLayout_dimension_ratio, 0f)

        array.recycle()
        canvasRounder = CanvasRounder(cornersHolder)
        updateOutlineProvider(cornersHolder)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            setLayerType(LAYER_TYPE_SOFTWARE, null)
        }
    }

    override fun onSizeChanged(
        currentWidth: Int,
        currentHeight: Int,
        oldWidth: Int,
        oldheight: Int
    ) {
        super.onSizeChanged(currentWidth, currentHeight, oldWidth, oldheight)
        canvasRounder.updateSize(currentWidth, currentHeight)
    }

    override fun draw(canvas: Canvas) = canvasRounder.round(canvas) {
        super.draw(it)
    }

    override fun dispatchDraw(canvas: Canvas) =
        canvasRounder.round(canvas) { super.dispatchDraw(it) }

    fun setCornerRadius(cornerRadius: Float, cornerType: CornerType = CornerType.ALL) {
        when (cornerType) {
            CornerType.ALL -> {
                canvasRounder.cornerRadius = cornerRadius
            }
            CornerType.TOP_LEFT -> {
                canvasRounder.topLeftCornerRadius = cornerRadius
            }
            CornerType.TOP_RIGHT -> {
                canvasRounder.topRightCornerRadius = cornerRadius
            }
            CornerType.BOTTOM_RIGHT -> {
                canvasRounder.bottomRightCornerRadius = cornerRadius
            }
            CornerType.BOTTOM_LEFT -> {
                canvasRounder.bottomLeftCornerRadius = cornerRadius
            }
            CornerType.R_BORDER_WIDTH -> {
                canvasRounder.rdBorderWidth = cornerRadius
            }
            CornerType.R_BORDER_COLOR -> {
                canvasRounder.rdBorderColor = cornerRadius
            }
        }
        updateOutlineProvider(cornerRadius)
        invalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var newWidthMeasureSpec = widthMeasureSpec
        var newHeightMeasureSpec = heightMeasureSpec
        if (dimensionRatio > 0f) {
            val width = MeasureSpec.getSize(widthMeasureSpec)
            val height = MeasureSpec.getSize(heightMeasureSpec)
            if (width != 0 && height == 0) {
                newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    (width * dimensionRatio).toInt(),
                    MeasureSpec.EXACTLY
                )
            } else if (width == 0 && height != 0) {
                newWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                    (height * dimensionRatio).toInt(),
                    MeasureSpec.EXACTLY
                )
            }
        }
        super.onMeasure(newWidthMeasureSpec, newHeightMeasureSpec)
    }
}
